<template>
  <div>
    <p>产品入驻之前提报表应该真实准确的填写 不能有虚报、瞒报</p>
    <p>
      厂家填写的各项资质必须官网可查，并真实准确 产品有明星代言必须提供证明授权关系 同品类全网保证机制最优 进出口商品需提供海关报关单
      有检测报告的厂家须提供检测报告且报告必须在官网可查 如国家级查询不到 能提供省级查询链路也可以 或能查询到检测机构也可以
      如全部查询不到按报告作假处理 营业执照 商标 备案凭证为必填项 如填写的各项资质有任意一项查询不到 直接打回商家次日可再次提报 营业执照 商标 备案凭证
      检测报告 保险单 等资质图片应可见各类编号以供查询 不清晰 没编号 没备案号的不予通过 可次日再次填报
    </p>
  </div>
  <div class="p-2">
    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" @click="handleAdd">添加品牌</el-button>
          </el-col>

          <!-- <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar> -->
        </el-row>
      </template>

      <el-table v-loading="loading" :data="brandList" @selection-change="handleSelectionChange">
        <el-table-column label="品牌名称" align="center" prop="brandName" />
        <el-table-column label="品牌商标注册证" align="center" prop="brandImage">
          <template #default="scope">
            <image-preview :src="scope.row.brandImage" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="商标授权使用证明" align="center" prop="usageImage">
          <template #default="scope">
            <image-preview :src="scope.row.usageImage" :width="50" :height="50" />
          </template>
        </el-table-column>


        <el-table-column label="使用授权品类" align="center" prop="usageCategory">
          <template #default="scope">
            {{ handleFilterMultiple(scope.row.usageCategory, hm_trademark) }}
          </template>
        </el-table-column>

        <el-table-column label="店铺销售授权" align="center" prop="sellImage">
          <template #default="scope">
            <image-preview :src="scope.row.sellImage" :width="50" :height="50" />
          </template>
        </el-table-column>

        <el-table-column label="销售授权品类" align="center" prop="sellCategory">
          <template #default="scope">
            {{ handleFilterMultiple(scope.row.sellCategory, hm_trademark) }}
          </template>
        </el-table-column>

        <el-table-column label="其他资质补充" align="center" prop="otherImage">
          <template #default="scope">
            <image-preview :src="scope.row.otherImage" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="联名款或其他授权文件" align="center" prop="jointlyImage">
          <template #default="scope">
            <image-preview :src="scope.row.jointlyImage" :width="50" :height="50" />
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button link type="primary" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改品牌对话框 -->


    <el-drawer class="hm-drawer" v-model="dialog.visible" size="40%" direction="rtl">
      <el-form ref="brandFormRef" :model="form" :rules="rules" label-width="160px">
        <el-form-item label="品牌名称" prop="brandName">
          <el-input v-model="form.brandName" placeholder="请输入品牌名称" clearable />
        </el-form-item>
        <el-form-item label="品牌商标注册证" label-width="160px" style="color:red" prop="brandImage">
          <image-upload :limit="1" v-model="form.brandImage" />
        </el-form-item>
        <el-form-item label="商标授权使用证明" label-width="160px" prop="usageImage">
          <image-upload :limit="1" v-model="form.usageImage" />
        </el-form-item>
        <el-form-item label="使用授权品类" prop="usageCategory">
          <el-select multiple v-model="form.usageCategory" placeholder="请选择授权品类" clearable>
            <el-option v-for="dict in hm_trademark" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="店铺销售授权" label-width="160px" prop="sellImage">
          <image-upload :limit="1" v-model="form.sellImage" />
        </el-form-item>
        <el-form-item label="销售授权品类" prop="sellCategory">
          <el-select multiple v-model="form.sellCategory" placeholder="请选择授权品类" clearable>
            <el-option v-for="dict in hm_trademark" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="其他资质补充" label-width="160px" prop="otherImage">
          <image-upload :limit="1" v-model="form.otherImage" />
        </el-form-item>
        <el-form-item label="联名款或其他授权文件" label-width="160px" prop="jointlyImage">
          <image-upload :limit="1" v-model="form.jointlyImage" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup name="Brand" lang="ts">
import { listBrand, getBrand, delBrand, addBrand, updateBrand } from '@/api/system/brand';
import { BrandVO, BrandQuery, BrandForm } from '@/api/system/brand/types';
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus';
import { handleFilterMultiple } from '@/utils/handleFilters';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_user_sex } = toRefs<any>(proxy?.useDict('sys_user_sex'));
const { hm_trademark } = toRefs<any>(proxy?.useDict('hm_trademark'));

// const handleExceed: UploadProps['onExceed'] = (files) => {
//   upload.value!.clearFiles()
//   const file = files[0] as UploadRawFile
//   file.uid = genFileId()
//   upload.value!.handleStart(file)
// }


const brandList = ref<BrandVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const brandFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: BrandForm = {
  id: undefined,
  brandName: undefined,
  brandImage: undefined,
  usageImage: undefined,
  sellImage: undefined,
  otherImage: undefined,
  jointlyImage: undefined,
  remark: undefined,
  usageCategory: undefined,
  sellCategory: undefined
};
const data = reactive<PageData<BrandForm, BrandQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: undefined,
    brandName: undefined,
    brandImage: undefined,
    usageImage: undefined,
    sellImage: undefined,
    otherImage: undefined,
    jointlyImage: undefined,
    remark: undefined,
    tenantId: undefined,
    delFlag: undefined,
    createDept: undefined,
    createBy: undefined,
    createTime: undefined,
    updateBy: undefined,
    updateTime: undefined,
  },
  rules: {
    brandName: [{
      required: true,
      message: '请输入品牌名称',
      trigger: 'blur'
    }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询品牌列表 */
const getList = async () => {
  loading.value = true;
  const res = await listBrand(queryParams.value);
  brandList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  brandFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: BrandVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  dialog.visible = true;
  dialog.title = '添加品牌';
  nextTick(() => {
    reset();
  });
};

/** 修改按钮操作 */
const handleUpdate = (row?: BrandVO) => {
  loading.value = true;
  dialog.visible = true;
  dialog.title = '修改品牌';
  nextTick(async () => {
    reset();
    const _id = row?.id || ids.value[0];
    const res = await getBrand(_id);
    loading.value = false;
    Object.assign(form.value, res.data);
    await getList();
  });
};

/** 提交按钮 */
const submitForm = () => {
  brandFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateBrand(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addBrand(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('修改成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: BrandVO) => {
  // const _ids = row?.id || ids.value;
  // await proxy?.$modal.confirm('是否确认删除品牌编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  // await delBrand(_ids);
  // proxy?.$modal.msgSuccess("删除成功");
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除名称为"' + row?.brandName + '"的数据项?');
  await delBrand(_ids);
  await getList();
  proxy?.$modal.msgSuccess('删除成功');

  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'system/brand/export',
    {
      ...queryParams.value
    },
    `brand_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>

<style lang="scss" scoped>
.item {
  color: blake;
}

:deep(.el-select) {
  width: 100%;
}
</style>
